package com.neulogistics.contrib.warehouse.infrastructure.persistence.converter;

import cn.hutool.json.JSONUtil;
import com.neulogistics.contrib.warehouse.domain.model.aggregate.CirculationOrder;
import com.neulogistics.contrib.warehouse.domain.model.enumeration.CirculationDetail;
import com.neulogistics.contrib.warehouse.domain.model.enumeration.CirculationStatus;
import com.neulogistics.contrib.warehouse.domain.model.enumeration.CirculationType;
import com.neulogistics.contrib.warehouse.domain.model.valueobject.GoodsItem;
import com.neulogistics.contrib.warehouse.infrastructure.persistence.orm.po.CirculationOrderPO;
import org.springframework.beans.BeanUtils;

public class CirculationOrderEntityPOConverter {
    public static CirculationOrderPO entityToPO(CirculationOrder circulationOrder){
        if(circulationOrder == null)
            return null;

        CirculationOrderPO circulationOrderPO = new CirculationOrderPO();
        // 复制普通属性
        BeanUtils.copyProperties(circulationOrder, circulationOrderPO);

        // json值对象转化
        if(circulationOrder.getGoodsItems() != null){
            circulationOrderPO.setGoodsItems(JSONUtil.parseArray(circulationOrder.getGoodsItems()));
        }

        // 枚举值转化
        if(circulationOrder.getCirculationType() != null){
            circulationOrderPO.setCirculationType(circulationOrder.getCirculationType().id);
        }
        if(circulationOrder.getCirculationDetail() != null){
            circulationOrderPO.setCirculationDetail(circulationOrder.getCirculationDetail().id);
        }
        if(circulationOrder.getCirculationStatus() != null){
            circulationOrderPO.setCirculationStatus(circulationOrder.getCirculationStatus().id);
        }

        return circulationOrderPO;
    }

    public static CirculationOrder POToEntity(CirculationOrderPO circulationOrderPO){
        if(circulationOrderPO == null)
            return null;

        CirculationOrder circulationOrder = new CirculationOrder();
        // 复制普通属性
        BeanUtils.copyProperties(circulationOrderPO, circulationOrder);
        // json值对象转化
        circulationOrder.setGoodsItems(JSONUtil.toList(
                circulationOrderPO.getGoodsItems(), GoodsItem.class));
        // 枚举值转化
        circulationOrder.setCirculationType(CirculationType.fromValue(
                circulationOrderPO.getCirculationType(), CirculationType.class));
        circulationOrder.setCirculationDetail(CirculationDetail.fromValue(
                circulationOrderPO.getCirculationDetail(), CirculationDetail.class));
        circulationOrder.setCirculationStatus(CirculationStatus.fromValue(
                circulationOrderPO.getCirculationStatus(), CirculationStatus.class));

        return circulationOrder;
    }
}
